<?php View::display('accounting/menu');?>
<?php $cis = $params['configInvoices'];?>
<!-- .head -->
<header class="head">
	<!-- ."main-bar -->
	<div class="main-bar">
		<div class="container-fluid">
			<div class="row-fluid">
				<div class="span12">
					<h3>
						<i class="icon-wrench"></i> การติดตั้งจากฝ่ายเทคนิค
					</h3>
				</div>
			</div>
			<!-- /.row-fluid -->
		</div>
		<!-- /.container-fluid -->
	</div>
	<!-- /.main-bar -->
</header>
<!-- /.head -->
<!-- #content -->
<div id="content">
	<!-- .outer -->
	<div class="container-fluid outer">
		<div class="row-fluid">
			<!-- .inner -->
			<div class="span12 inner">
				<!--Begin Datatables-->
				<div class="row-fluid">
					<div class="span12">
						<div class="box">
							<header>
								<h5>การแจ้งติดตั้ง</h5>
							</header>
							<div class="body">
								<table class="table table-bordered dataTable responsive">
									<thead>
										<tr>
											<th>invoice id</th>
											<th>pcakage</th>
											<th>info</th>
											<th>status</th>
										</tr>
									</thead>
									<tbody>
                                        <?php foreach($cis as $key => $ci){
                                            $status = $ci['status']==1? 'ติดตั้งแล้ว': 'รอการติดตั้ง';
                                            $statusClass = $ci['status']==1? 'success': 'warning';
                                            $package = $ci['invoice']['package']['type'].' - '.$ci['invoice']['package']['name'];
                                            $invoice = sprintf('<a targer="_blank" href="old.php?page=invoice&id=%s">%s</a>', $ci['invoice']['id'], $ci['invoice']['id']);
                                        ?>
										<tr class="list-row <?php echo $statusClass;?>" rowId="<?php echo $ci['id'];?>" invoiceId="<?php echo $ci['invoiceId'];?>">
											<td><?php echo $invoice;?></td>
											<td><?php echo $package;?></td>
											<td><?php echo nl2br($ci['invoice']['info']);?></td>
											<td><?php echo $status;?></td>
										</tr>
									    <?php }?>
									</tbody>
								</table>
							</div>
						</div>
					</div>
				</div>
			</div>
		</div>
		<!--Begin Datatables-->
	</div>
</div>
<script type="text/template" class="modalAddProduct">
	<div class="modal hide fade">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
			<h3>ยืนยันการจ่ายเงิน + สร้าง product (ลูกค้าใหม่)</h3>
		</div>
		<div class="modal-body">
			<form>
				<label>package</label>
				<div class="alert alert-info package"></div>
				<label>account</label>
				<div class="alert alert-info email"></div>
				<label>month</label>
				<div class="alert alert-info month"></div>
				<label>price</label>
				<div class="alert alert-info price"></div>
				<div class="optional-product"></div>
				<label>info</label>
				<textarea class="info"></textarea>
			</form>
		</div>
		<div class="modal-footer">
			<a href="#" class="btn cancel">Cancel</a>
			<a href="#" class="btn btn-primary paidNew">Submit</a>
		</div>
	</div>
</script>
<script type="text/javascript">
$(function(){
	var oTable = $('.dataTable').dataTable({"bFilter": false, "bInfo": false, "bSort": false,
		sDom: "<'pull-right'l>t<'row-fluid'<'span6'f><'span6'p>>",
        sPaginationType: "bootstrap",
        oLanguage: {
            "sLengthMenu": "Show _MENU_ entries"
        }
    });

	function delRow(row){
		$(row).fadeOut(function(){
			row = $(row).get(0);
			oTable.fnDeleteRow(oTable.fnGetPosition(row));
		});
	}
	
	$('.list-row')
	.delegate('.addProduct', 'click', function(event){
		var dTarget = $(event.delegateTarget);
		var target = $(event.target);
		
		var id = dTarget.attr('rowId');
		var invoiceId = dTarget.attr('invoiceId');
		
		target.button('loading');
		$.get('old.php?page=accounting/invoice.rest', {id: invoiceId}, function(data){
			target.button('reset');
			var modal = $($('.modalAddProduct').html());
			var sendData = {};
			sendData.id = id;
			sendData.invoiceId = invoiceId;
			
			modal.modal();
			modal.on('hidden', function(data){
				modal.remove();
			});

			$('.cancel', modal).click(function(event){
				$('.close', modal).click();
			});
			
			$('.package', modal).html(
				'<strong>'+data.type+'</strong><br />'+
				'<small>'+data.name+'</small>'
			);
			sendData.packageId = data.packageId;

			$('.email', modal).html(
				'<a target="_blank" href="old.php?page=accounting/account/detail&id='+data.account.id+'">'+data.account.email+'</a>'
			);
			sendData.accountId = data.account.id;

			$('.month', modal).html(data.month+' เดือน');
			$('.price', modal).html(data.price+' บาท');
			sendData.month = data.month;
			sendData.price = data.price;

			$('.info', modal).val(data.info);

			sendData.type='colo';
			if(data.type=='vps'){
				var os;
				if(data.os == 2){ os = 'Linux'; }
				else if(data.os == 3){ os = 'Window'; }
				
				$('.optional-product', modal).append('<label>OS</label>');
				$('.optional-product', modal).append('<div class="alert alert-info">'+os+'</div>');

				sendData.os = data.os;
				sendData.type='vps';
			}
			else if(data.type=='hosting'){
				$('.optional-product', modal).append('<label>Domain</label>');
				$('.optional-product', modal).append('<div class="alert alert-info">'+data.domain+'</div>');
				sendData.domain = data.domain;
				sendData.type='hosting';
			}
			
			var sending = false;
			$('.paidNew', modal).click(function(event){
				if(sending) return;
				var button = $(this);

				sendData.info = $('.info').val();
				
				button.button('loading');
				sending = true;
				$.post('old.php?page=accounting/configInvoice.addProduct', sendData, function(data){
					sending = false;
					button.button('reset');
					if(data.success){
						//target.remove();
						modal.modal('hide');
						delRow(dTarget);
					}
				}, 'JSON');
			});
		}, 'JSON');
	});
});
</script>